--[[
Copyright:www.vxinyou.com
Author:马柏威
CreateTime:2014-12-11
Description:图形对象类，作为骨骼动画，文本，特效等等对象的父节点
]]

local clsGraphObject = Class("lua/cc/clsNode.lua"):Inherit()

------------------------------public------------------------------
------------------------------------------------------------------

--对象朝向
--local direction = {['left'] = -1,['right'] = 1}
--Function:得到资源ID
function clsGraphObject:getResID()
	return self.resID
end

--Function:设置骨骼动画回逻辑调函数 
--Input:函数参考：Event(eventType, movementType, movementID)
function clsGraphObject:setMovementEvent(event)
	if event then
		self.onMovementCallbackFun = event
	end
end
--Function:获得骨骼动画回调逻辑函数
function clsGraphObject:getMovementEvent()
	return self.onMovementCallbackFun
end
--Function:执行骨骼动画回调逻辑函数
function clsGraphObject:onMovementEvent(armatureBack,movementType,movementID)
	if self.onMovementCallbackFun then
		self.onMovementCallbackFun(armatureBack,movementType,movementID)
	end
end

--Function:卸载骨骼动画
function clsGraphObject:unloadAnimate(path)
	self.curAnimate:destroy()
end
--Output:当前播放的骨骼动画的名称
function clsGraphObject:getCurrentMovementID()
	return self.curAnimate:getCurrentMovementID()
end

--Function:播放骨骼动画
function clsGraphObject:runAnimation(name)
	if self.curAnimate and name then
		self.curAnimate:playWithName(name)
		self.curMovementName = name
	end
end

--Function:停止播放骨骼动画
function clsGraphObject:stopAnimation()
	if self.curAnimate then
		self.curAnimate:stop()
	end
end

--Function:暂停播放骨骼动画
function clsGraphObject:pauseAnimation()
	if self.curAnimate then
		self.curAnimate:pause()
	end
end


--Function:得到骨骼动画的包围盒
function clsGraphObject:getBoundingBox(part)
	if self.curAnimate then
		local animateBoundingBox = self.curAnimate:getBoundingBox(part)
		local selfPosX, selfPosY = self:getPosition()
		return CCRectMake(selfPosX + animateBoundingBox:getMinX(),
		selfPosY + animateBoundingBox:getMinY() - self.curAnimate.boundingBoxY /2,
		self.curAnimate.boundingBoxX,
		self.curAnimate.boundingBoxY)
	end
end
--Function:设置颜色
--Input：ccc3(r,g,b)
function clsGraphObject:setColor(color)
	if self.curAnimate and color then
		self.curAnimate:setColor(color)
	end
end

--Function:改变颜色(持续时间，Color3B)
function clsGraphObject:setHittedColor(color,duration)
	if self.curAnimate and color then
		self.curAnimate:setColor(color)
		local function restoreColor()
			self.curAnimate:setColor(ccc3(255, 255, 255))
		end
		if self.colorAction then
			self:stopAction(self.colorAction)
		end
		self.colorAction = MOD_ACTION.f_delay_do(self:getCOObj(), restoreColor, nil, duration)
	end
end

--Function:重新加载骨架
function clsGraphObject:reloadAnimate(args)
	if self.curAnimate then
		self:unloadAnimate()
		self:loadAnimate(args)
		if self:getDirection() > 0 then
			self:turnRight()
		else
			self:turnLeft()
		end
		--self:runAnimation(self.curMovementName)
	end
end
--Function:获得节点原始大小
function clsGraphObject:getContentSize()
	if self.curAnimate then
		return self.curAnimate:getContentSize()
	end
end
--Function:设置坐标点
function clsGraphObject:setPosition(x,y)
	if self._root and x and y then
		self._root:setPosition(x,y)
	end
end
--Function：获得坐标点
function clsGraphObject:getPosition()
	if self._root then
		return self._root:getPosition()
	end
end
--Function:播放动画 
--Input:动作名，播放时间（空表示正常播放） 是否循环
function clsGraphObject:play(name,playTime,loop)
	if self.curAnimate and name then
		self.curAnimate:playWithName(name,playTime,loop)
	end
end
--Function:设置透明度
function clsGraphObject:setOpacity(num)
	if self.curAnimate and num then
		self.curAnimate:setOpacity(num)
	end
end
--Function:左右翻转
function clsGraphObject:setFlipX()
	if self.curAnimate then
		self.curAnimate:setFlipX()
	end
end
--Function：是否面向左
function clsGraphObject:isFaceLeft()
	return self.nowDirection == -1
end
--Function：设置X缩放系数
function clsGraphObject:setScaleX(fScale)
	if self._son_list and fScale then		
		self._root:setScaleX(fScale)
		for k, son_node in pairs(self._son_list) do
			if son_node.isTurnAccordingToParent then
				if not son_node:isTurnAccordingToParent() then
					son_node:setScaleX(fScale)
				end
			end
		end
	end
	--[[if self.curAnimate and fScale then
		self.curAnimate:setScaleX(fScale)
	end--]]
end
--Function：是否跟着父节点旋转
function clsGraphObject:isTurnAccordingToParent()
	return self.turnAccordingToParent
end
--Function：获得X缩放系数
function clsGraphObject:getScaleX()
	if self.curAnimate then
		return self.curAnimate:getScaleX()
	end
end

--Function:换武器
--Input:资源路径，武器名字
function clsGraphObject:changeWeapon(res,weaponName)
	if self.curAnimate and res and weaponName then
		self.curAnimate:changeSkin(res,"weapon",weaponName)
	end
end
--Function：转左
function clsGraphObject:turnLeft()
	self:setScaleX(-1 * self.initialDirection)
	self.nowDirection = -1
end
--Function：转右
function clsGraphObject:turnRight()
	self:setScaleX(1 * self.initialDirection)
	self.nowDirection = 1
end
--Function：获得面向方向
function clsGraphObject:getDirection()
	if self.nowDirection then
		return self.nowDirection -- -1为左，1为右
	end
end
--Function：设置面向方向
function clsGraphObject:setDirection(direct)
	if direct < 0 then
		self:turnLeft()
	else
		self:turnRight()
	end
end
--Output:骨骼动画对象
function clsGraphObject:getAnimate()
	return self.curAnimate
end

function clsGraphObject:getBBHeight()
	return self.curAnimate:getBBHeight()
end

function clsGraphObject:getBBWidth()
	return self.curAnimate:getBBWidth()
end

function getClass()
	return clsGraphObject
end

------------------------------private-----------------------------
------------------------------------------------------------------


function clsGraphObject:create(args)
	self:ImportClass()
	self.curAnimate = nil --当前显示的骨骼动画
	self.initialDirection = 1
	self.nowDirection = 1
	self.curMovementName = "stand"
	self.event = self.onMovementEvent
	self.args = args
	self.resID = args.resID
	self.propertyID = args.propertyID or nil
	if args.propertyID then
		self.resID = args.resID or self.oMonsterCfg:getResID(self.propertyID)
	end
	self.colorAction = nil	
	self.turnAccordingToParent = true
	if args.turnAccordingToParent ~= nil then
		self.turnAccordingToParent = args.turnAccordingToParent
	end
	
	if self.resID and self.oResCfg:getResourcesOrientByID(self.resID) then
		if self.oResCfg:getResourcesOrientByID(self.resID) == 0 then
			self.initialDirection = -1
			self.nowDirection = self.initialDirection
		end
		self.zOrder = self.oResCfg:getResourcesOffsetForZOrderByID(self.resID)
	end
	--MOD_DEBUG.d("clsGraphObject:create")
	self._root = CCNode:create()
	if self.zOrder then
		self:setZOrder(self.zOrder)
	end
	if not args.resID then
		args.resID = self.resID
	end
	if args.resID then
		self:loadAnimate(args)
	end
end

--Function:导入需要用到的类对象
function clsGraphObject:ImportClass()
	self.oSkillEffectFunc = Class("lua/game/combat/clsSkillEffectFunc.lua"):getInstance()
	self.oGameWorld = Class("lua/game/clsGameWorld.lua"):getInstance()
	self.oSkillCfg = Class("lua/game/combat/clsSkillCfg.lua"):getInstance()
	self.oMonsterCfg = Class("lua/game/clsMonsterCfg.lua"):getInstance()
	self.oResCfg = Class("lua/game/clsResCfg.lua"):getInstance()
	--self.oAudioManager = Class("lua/game/clsAudioManager.lua"):getInstance()
	self.oObjectFactory = Class("lua/game/clsObjectFactory.lua"):getInstance()
end

--Function:加载骨骼动画
function clsGraphObject:loadAnimate(args)
	local animateConfig = clone(args)
	animateConfig.x = 0
	animateConfig.y = 0
	local function fun(armatureBack,movementType,movementID)
		self:event(armatureBack,movementType,movementID)
	end
	animateConfig.event = fun
	self.curAnimate = Class("lua/cc/clsArmature.lua"):New(self,animateConfig)
end
